# **********第1题：三乐购物系统**********
# 使用系统需要登录验证，进入系统后，输入购物预算信息
# 用户可以任意查看商品信息和已购买信息
# 用户根据品编商号购买商品，购买时，检测余额是否够，够就直接扣款，不够就提醒
# 可随时退出（输入exit），退出后，打印已购买商品和余额
# 要求：用尽可能友好的方式输出，封装函数
# {'F00001':{'name':'苹果','price':1.2},'F00002':{'name':'香蕉','price':5.5},}

print("********** Welcome to “三乐购物系统”！**********")

# 登录验证
def login_verify(username='xll',password='xll123'):
    for i in range(3):
        username = input("请输入用户名：")
        password = input("请输入密码：")
        if username == 'xll' and password == "xll123":
            print("验证成功！")
            break
        else:
            print(f"验证失败！你还剩{2-i}次机会！")
    else:
        print("很遗憾，你的身份验证已超过3次，请稍后再试！")
login_verify()

# 小店商品清单
def product_list():
    print("**********小店商品清单**********")
    global products
    products = {"F001":{"name":"apple","price":6},"F002":{"name":"banana","price":4},
                "F003": {"name": "strawberry", "price": 13},"F004":{"name":"cherry","price":41},
                "F005": {"name": "peach", "price": 7},"F006":{"name":"watermelon","price":2}}
    print(products)

# 顾客购买商品
def buy_goods():
    buy_products = []
    while True:
        global budget
        budget = input("请输入你本次的购物预算：")
        if budget.isdigit():
            budget = int(budget)
            break
        elif budget == "exit":
            exit()
        else:
            print("输入有误，请输入有效数字")

    product_list()

    while True:
        product_id = input("请输入你要购买的商品编号：")
        if product_id == "exit":
            exit()
        consumption_sum = products[product_id]['price']
        if consumption_sum <= budget:
            budget -= consumption_sum
            print(f"你所消费的金额是：{consumption_sum}，还剩金额：{budget}")
            buy_products.append(products[product_id])
            print(f"**********订单详情*********\n"
                  f"{buy_products}")
        else:
            print("你的预算不够，请充值！")
            budget1 = int(input("请充值："))
            budget += budget1
            continue
buy_goods()

